package com.yst.sqlbuilder.mathExpression;

import com.yst.lambda.TableFieldFunc;
import com.yst.sqlbuilder.GenerateParam;
import com.yst.sqlbuilder.ISelectFieldExpression;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

public abstract class FieldExpression<T> implements ISelectFieldExpression<T> {

    private final TableFieldFunc<T,?> field;
    private String alias;

    protected abstract String formatField(String field);

    public FieldExpression(TableFieldFunc<T,?> field, String alias){
        this.field=field;
        this.alias=alias;
    }


    public FieldExpression(TableFieldFunc<T,?> field) {
        this(field,"");
    }

    public FieldExpression<T> setAlias(String alias){
        this.alias=alias;
        return this;
    }

    @Override
    public String getAlias() {
        return alias;
    }

    @Override
    public String generateExpression(GenerateParam paramRegister) {
        String fieldName= getEntitiesConfig().getFieldName(field);
        if (StringUtils.compareIgnoreCase(fieldName,getAlias())==0){
            return formatField(fieldName).concat(" AS ").concat(fieldName);
        }
        return formatField(fieldName).concat(" AS ").concat(getAlias());
    }
}
